<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>L2LB: interface.h Source File</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
<link href="tabs.css" rel="stylesheet" type="text/css">
</head><body>
<!-- Gerado por Doxygen 1.5.6 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.html"><span>Página&nbsp;principal</span></a></li>
      <li><a href="annotated.html"><span>Estruturas&nbsp;de&nbsp;dados</span></a></li>
      <li class="current"><a href="files.html"><span>Ficheiros</span></a></li>
    </ul>
  </div>
<h1>interface.h</h1><a href="interface_8h.html">Ir para a documentação deste ficheiro.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00008"></a>00008 <span class="preprocessor">#ifndef INTERFACE_H_</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span><span class="preprocessor">#define INTERFACE_H_</span>
<a name="l00010"></a>00010 <span class="preprocessor"></span>
<a name="l00011"></a>00011 <span class="preprocessor">#include &lt;linux/proc_fs.h&gt;</span>      <span class="comment">/* Necessary because we use the proc fs */</span>
<a name="l00012"></a>00012 <span class="preprocessor">#include &lt;asm/uaccess.h&gt;</span>        <span class="comment">/* for copy_from_user */</span>
<a name="l00013"></a>00013 <span class="preprocessor">#include "<a class="code" href="cluster_8h.html" title="Arquivo responsável por gerenciar (incluir/remover/buscar) os servidores do cluster...">cluster.h</a>"</span>
<a name="l00014"></a>00014 
<a name="l00015"></a><a class="code" href="interface_8h.html#cb57117519ba69a9fa2bc2ea4661ccaf">00015</a> <span class="preprocessor">#define PROCFS_MAX_SIZE         1024</span>
<a name="l00016"></a><a class="code" href="interface_8h.html#b1dafda7cd89906a3be27b9bcd279a0b">00016</a> <span class="preprocessor"></span><span class="preprocessor">#define PROCFS_NAME             "nlb"</span>
<a name="l00017"></a>00017 <span class="preprocessor"></span>
<a name="l00021"></a><a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d">00021</a> <span class="keyword">static</span> <span class="keyword">struct </span>proc_dir_entry *<a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>;
<a name="l00022"></a>00022 
<a name="l00033"></a><a class="code" href="interface_8h.html#5ed0ad92bc73cd43e19e29f0ff7aa8b4">00033</a> <span class="keywordtype">void</span> <a class="code" href="interface_8h.html#5ed0ad92bc73cd43e19e29f0ff7aa8b4" title="Adiciona um servido ao cluster.">cmd_add_node</a>(<span class="keywordtype">char</span> *args)
<a name="l00034"></a>00034 {
<a name="l00035"></a>00035         <span class="keywordtype">int</span> id;
<a name="l00036"></a>00036         <span class="keywordtype">char</span> vlan_ip_c[16];
<a name="l00037"></a>00037         u32 vlan_ip;
<a name="l00038"></a>00038         
<a name="l00039"></a>00039         sscanf(args, <span class="stringliteral">"%d,%16s"</span>, &amp;<span class="keywordtype">id</span>, (<span class="keywordtype">char</span> *)&amp;vlan_ip_c);
<a name="l00040"></a>00040         
<a name="l00041"></a>00041         <a class="code" href="utils_8h.html#7a719668dc1cee29bc80d85386ad478e" title="Parse um endereço IPv4.">parse_ipv4_addr</a>(vlan_ip_c, &amp;vlan_ip);
<a name="l00042"></a>00042         
<a name="l00043"></a>00043         <a class="code" href="cluster_8h.html#a568a7669c4ee6f27338231d23575c13" title="Registra um novo servidor do cluster.">add_cluster_node</a>(<span class="keywordtype">id</span>, vlan_ip);
<a name="l00044"></a>00044 }
<a name="l00045"></a>00045 
<a name="l00053"></a><a class="code" href="interface_8h.html#f3fa25a0ac2d61c6d5700b9bf0a0132b">00053</a> <span class="keywordtype">void</span> <a class="code" href="interface_8h.html#f3fa25a0ac2d61c6d5700b9bf0a0132b" title="Indica na lista de servidores, qual deles é servidor atual.">cmd_my_node</a>(<span class="keywordtype">char</span> *args)
<a name="l00054"></a>00054 {
<a name="l00055"></a>00055         <span class="keywordtype">int</span> id;
<a name="l00056"></a>00056         <span class="keyword">struct </span><a class="code" href="structcluster__node.html" title="Nó do cluster.">cluster_node</a> *<a class="code" href="cluster_8h.html#39e3ce09327c335c6a58f152f9d8fbbf" title="Nó do cluster.">my_node</a>;
<a name="l00057"></a>00057         
<a name="l00058"></a>00058         sscanf(args, <span class="stringliteral">"%d"</span>, &amp;<span class="keywordtype">id</span>);
<a name="l00059"></a>00059         
<a name="l00060"></a>00060         my_node = <a class="code" href="cluster_8h.html#4e116ec9395a6720c5f84de3e28b25a9" title="Busca um servidor do cluster pelo ID.">get_node_by_id</a>(<span class="keywordtype">id</span>);
<a name="l00061"></a>00061         
<a name="l00062"></a>00062         <span class="keywordflow">if</span>(!my_node) {
<a name="l00063"></a>00063                 printk( KERN_ALERT <span class="stringliteral">"Servidor NAO encontrado!\n"</span> );
<a name="l00064"></a>00064                 <span class="keywordflow">return</span>;
<a name="l00065"></a>00065         }
<a name="l00066"></a>00066         
<a name="l00067"></a>00067         <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#7ccd7197eb601ffee1b0f6209578e9a2">my_node</a> = my_node;
<a name="l00068"></a>00068         
<a name="l00069"></a>00069         <a class="code" href="heartbeat_8h.html#8c2bcf74f2baf343f7ee6f8cf8d26aaf" title="Re-cria a mensagem de envio do heartbeat.">hb_rebuild_msg</a>();
<a name="l00070"></a>00070 }
<a name="l00071"></a>00071 
<a name="l00080"></a><a class="code" href="interface_8h.html#955b05c0f5c5662d771a12b4881c479a">00080</a> <span class="keywordtype">int</span> <a class="code" href="interface_8h.html#955b05c0f5c5662d771a12b4881c479a" title="Funçao chamada quando o arquivo /proc/nlb é lido.">procfile_read</a>(<span class="keywordtype">char</span> *buffer, <span class="keywordtype">char</span> **buffer_location, off_t offset, <span class="keywordtype">int</span> buffer_length, <span class="keywordtype">int</span> *eof, <span class="keywordtype">void</span> *data)
<a name="l00081"></a>00081 {
<a name="l00082"></a>00082         <span class="comment">//int read_proc(char *page, char **start, off_t off, int count,</span>
<a name="l00083"></a>00083         <span class="comment">//            int *eof, void *data)</span>
<a name="l00084"></a>00084         <span class="keywordtype">int</span> len = 0;
<a name="l00085"></a>00085         off_t begin = 0;
<a name="l00086"></a>00086         <span class="keyword">struct </span><a class="code" href="structcluster__node.html" title="Nó do cluster.">cluster_node</a> *node = 0;
<a name="l00087"></a>00087 
<a name="l00088"></a>00088         node = <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#dc003d7c81ab0cee1802cb6742a2de98">head</a>;
<a name="l00089"></a>00089         len += sprintf(buffer, <span class="stringliteral">"Node list:\n"</span>);
<a name="l00090"></a>00090 
<a name="l00091"></a>00091         <span class="keywordflow">while</span>( node ) {
<a name="l00092"></a>00092                 
<a name="l00093"></a>00093                 len += sprintf(buffer + len, <span class="stringliteral">"ID:%d, VLAN-IP:%u.%u.%u.%u"</span>, node-&gt;<a class="code" href="structcluster__node.html#7f6ec5e953944d0fce058aa0490cf8b3">id</a>, NIPQUAD(node-&gt;<a class="code" href="structcluster__node.html#6a7ba17a6dfff66ba64359895b90c16e">vlan_ip</a>) );
<a name="l00094"></a>00094                 
<a name="l00095"></a>00095                 <span class="keywordflow">if</span>(node == <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#0790d37c29c68e44e2e6aa3c0d46bb70">next_serving</a>)
<a name="l00096"></a>00096                         len += sprintf(buffer + len, <span class="stringliteral">" (Next)"</span> );
<a name="l00097"></a>00097                 <span class="keywordflow">if</span>(node == <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#7ccd7197eb601ffee1b0f6209578e9a2">my_node</a>)
<a name="l00098"></a>00098                         len += sprintf(buffer + len, <span class="stringliteral">" (My Node)"</span> );
<a name="l00099"></a>00099                 len += sprintf(buffer + len, <span class="stringliteral">"\n"</span> );
<a name="l00100"></a>00100                 
<a name="l00101"></a>00101                 <span class="keywordflow">if</span> (len+begin &gt; offset+buffer_length)
<a name="l00102"></a>00102                         <span class="keywordflow">goto</span> done;
<a name="l00103"></a>00103                 <span class="keywordflow">if</span> (len+begin &lt; offset) {
<a name="l00104"></a>00104                         begin += len;
<a name="l00105"></a>00105                         len = 0;
<a name="l00106"></a>00106                 }
<a name="l00107"></a>00107                 
<a name="l00108"></a>00108                 node = node-&gt;<a class="code" href="structcluster__node.html#fbbc66175da004fcb51b8541ec392ce5">next</a>;
<a name="l00109"></a>00109                 <span class="keywordflow">if</span>(node == <a class="code" href="cluster_8h.html#1917b2709741dea7ce5cf6ea441686ed" title="Cluster.">my_cluster</a>.<a class="code" href="structcluster.html#dc003d7c81ab0cee1802cb6742a2de98">head</a>) 
<a name="l00110"></a>00110                         <span class="keywordflow">break</span>;
<a name="l00111"></a>00111         }
<a name="l00112"></a>00112 
<a name="l00113"></a>00113         *eof = 1;
<a name="l00114"></a>00114 done:
<a name="l00115"></a>00115         <span class="keywordflow">if</span> (offset &gt;= len+begin)
<a name="l00116"></a>00116                 <span class="keywordflow">return</span> 0;
<a name="l00117"></a>00117         *buffer_location = buffer + (offset-begin);
<a name="l00118"></a>00118         <span class="keywordflow">return</span> ((buffer_length &lt; begin+len-offset) ? buffer_length : begin+len-offset);
<a name="l00119"></a>00119 }
<a name="l00120"></a>00120 
<a name="l00129"></a><a class="code" href="interface_8h.html#140b602157d344e18a9e8750d96d7d81">00129</a> <span class="keywordtype">int</span> <a class="code" href="interface_8h.html#140b602157d344e18a9e8750d96d7d81" title="Função chamado quando se escreve no arquivo /proc/nlb.">procfile_write</a>(<span class="keyword">struct</span> file *file, <span class="keyword">const</span> <span class="keywordtype">char</span> *buffer, <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> count, <span class="keywordtype">void</span> *data)
<a name="l00130"></a>00130 {
<a name="l00131"></a>00131         <span class="keywordtype">char</span> my_procfs_buffer[<a class="code" href="interface_8h.html#cb57117519ba69a9fa2bc2ea4661ccaf">PROCFS_MAX_SIZE</a>]; 
<a name="l00132"></a>00132         <span class="keywordtype">char</span> command[<a class="code" href="interface_8h.html#cb57117519ba69a9fa2bc2ea4661ccaf">PROCFS_MAX_SIZE</a>];
<a name="l00133"></a>00133         <span class="keywordtype">char</span> args[<a class="code" href="interface_8h.html#cb57117519ba69a9fa2bc2ea4661ccaf">PROCFS_MAX_SIZE</a>];
<a name="l00134"></a>00134         
<a name="l00135"></a>00135         <span class="comment">//get buffer size</span>
<a name="l00136"></a>00136         <span class="keywordflow">if</span> (count &gt; <a class="code" href="interface_8h.html#cb57117519ba69a9fa2bc2ea4661ccaf">PROCFS_MAX_SIZE</a>) {
<a name="l00137"></a>00137                 count = <a class="code" href="interface_8h.html#cb57117519ba69a9fa2bc2ea4661ccaf">PROCFS_MAX_SIZE</a>;
<a name="l00138"></a>00138         }
<a name="l00139"></a>00139         
<a name="l00140"></a>00140         <span class="comment">//write data to the buffer</span>
<a name="l00141"></a>00141         <span class="keywordflow">if</span> ( copy_from_user(my_procfs_buffer, buffer, count) ) {
<a name="l00142"></a>00142                 <span class="keywordflow">return</span> -EFAULT;
<a name="l00143"></a>00143         }
<a name="l00144"></a>00144 
<a name="l00145"></a>00145         sscanf(my_procfs_buffer, <span class="stringliteral">"%s :%s"</span>, command, args);
<a name="l00146"></a>00146         
<a name="l00147"></a>00147         <span class="keywordflow">if</span>(strcmp(command,<span class="stringliteral">"add-node"</span>) == 0)
<a name="l00148"></a>00148                 <a class="code" href="interface_8h.html#5ed0ad92bc73cd43e19e29f0ff7aa8b4" title="Adiciona um servido ao cluster.">cmd_add_node</a>(args);
<a name="l00149"></a>00149         <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strcmp(command,<span class="stringliteral">"my-node"</span>) == 0)
<a name="l00150"></a>00150                 <a class="code" href="interface_8h.html#f3fa25a0ac2d61c6d5700b9bf0a0132b" title="Indica na lista de servidores, qual deles é servidor atual.">cmd_my_node</a>(args);
<a name="l00151"></a>00151         <span class="keywordflow">else</span> <span class="keywordflow">if</span>(strcmp(command,<span class="stringliteral">"balance-protocol"</span>) == 0)
<a name="l00152"></a>00152                 printk( KERN_ALERT <span class="stringliteral">"Not yet implemented! (%s)\n"</span>, command );
<a name="l00153"></a>00153         <span class="keywordflow">else</span>
<a name="l00154"></a>00154                 printk( KERN_ALERT <span class="stringliteral">"Comando inválido! (%s)\n"</span>, command );
<a name="l00155"></a>00155         
<a name="l00156"></a>00156         <span class="keywordflow">return</span> count;
<a name="l00157"></a>00157 }
<a name="l00158"></a>00158 
<a name="l00162"></a><a class="code" href="interface_8h.html#65a3f35a0907ebf967e1f54265516827">00162</a> <span class="keywordtype">int</span> <a class="code" href="interface_8h.html#65a3f35a0907ebf967e1f54265516827" title="Inicializa a interface do cluster criando o arquivo /proc/nlb.">init_proc_interface</a>(<span class="keywordtype">void</span>)
<a name="l00163"></a>00163 {
<a name="l00164"></a>00164         <span class="comment">//create the /proc file</span>
<a name="l00165"></a>00165         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a> = create_proc_entry(<a class="code" href="interface_8h.html#b1dafda7cd89906a3be27b9bcd279a0b">PROCFS_NAME</a>, 0644, NULL);
<a name="l00166"></a>00166         
<a name="l00167"></a>00167         <span class="keywordflow">if</span> (!<a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>) {
<a name="l00168"></a>00168                 remove_proc_entry(<a class="code" href="interface_8h.html#b1dafda7cd89906a3be27b9bcd279a0b">PROCFS_NAME</a>, &amp;proc_root);
<a name="l00169"></a>00169                 printk(KERN_ALERT <span class="stringliteral">"Error: Could not initialize /proc/%s\n"</span>, <a class="code" href="interface_8h.html#b1dafda7cd89906a3be27b9bcd279a0b">PROCFS_NAME</a>);
<a name="l00170"></a>00170                 <span class="keywordflow">return</span> -ENOMEM;
<a name="l00171"></a>00171         }
<a name="l00172"></a>00172 
<a name="l00173"></a>00173         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>-&gt;read_proc = <a class="code" href="interface_8h.html#955b05c0f5c5662d771a12b4881c479a" title="Funçao chamada quando o arquivo /proc/nlb é lido.">procfile_read</a>;
<a name="l00174"></a>00174         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>-&gt;write_proc = <a class="code" href="interface_8h.html#140b602157d344e18a9e8750d96d7d81" title="Função chamado quando se escreve no arquivo /proc/nlb.">procfile_write</a>;
<a name="l00175"></a>00175         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>-&gt;owner = THIS_MODULE;
<a name="l00176"></a>00176         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>-&gt;mode = S_IFREG | S_IRUGO;
<a name="l00177"></a>00177         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>-&gt;uid = 0;
<a name="l00178"></a>00178         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>-&gt;gid = 0;
<a name="l00179"></a>00179         <a class="code" href="interface_8h.html#86ed009339cd472e0414306129f6650d" title="Esta estrutura mantém informações sobre o arquivo /proc/nlb.">my_proc_file</a>-&gt;size = 37;
<a name="l00180"></a>00180         
<a name="l00181"></a>00181         <span class="keywordflow">return</span> 0;
<a name="l00182"></a>00182 }
<a name="l00183"></a>00183 
<a name="l00184"></a>00184 <span class="preprocessor">#endif </span><span class="comment">/*INTERFACE_H_*/</span>
</pre></div></div>
<hr size="1"><address style="text-align: right;"><small>Gerado em Wed Sep 10 23:05:55 2008 para L2LB por&nbsp;
<a href="http://www.doxygen.org/index.html">
<img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address>
</body>
</html>
